package com.mytrip.webapp.action.register;


import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.mytrip.base.action.StrutsActionAdapter;
import com.mytrip.model.Userinfo;
import com.mytrip.service.UserManager;
import com.mytrip.util.LoginUtil;
import com.mytrip.webapp.filter.CtxSessionBag;

public class RegisterAction extends StrutsActionAdapter {

	private static final long serialVersionUID = -2360219702060303467L;

	private Userinfo user = new Userinfo();

	private String returnUrl;

	private int errorCode;

	private UserManager userManager;

	public int getErrorCode() {
		return errorCode;
	}
	
	public String goReg() throws Exception{
		return "goReg";
	}

	public String regAdd() throws Exception {
		
		HttpServletRequest request = ServletActionContext.getRequest();
		String userName = request.getParameter("email");
		String password = request.getParameter("pwd");
		String realName = request.getParameter("realName");
		user.setEmail(userName);
		user.setPwd(password);
		user.setRealName(realName);
		
		try {
			userManager.updatePasswordAndEmail(user);
		} catch (Exception e) {
			return "system_error";
		}
		
		CtxSessionBag.getSessionBag().setUserinfo(user);
		LoginUtil.saveCookie(user.getEmail(), user.getPwd(),LoginUtil.COOKIE_FRONT);
		
		return SUCCESS;
	}

	public String getReturnUrl() {
		return returnUrl;
	}

	public void setReturnUrl(String returnUrl) {
		this.returnUrl = returnUrl;
	}

	public Userinfo getUser() {
		return user;
	}

	public void setUser(Userinfo user) {
		this.user = user;
	}

	public UserManager getUserManager() {
		return userManager;
	}

	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

}
